package utils

import (
	"bytes"
	"encoding/json"
)

type ResponseCache struct {
	Header map[string]string
	Body   []byte
}

const splitChar = "!@#$%^&*()123456789"

func (r *ResponseCache) Decode() (string, bool) {
	marshal, err := json.Marshal(r.Header)
	if err != nil {
		return "", false
	}
	buf := bytes.NewBuffer(marshal)
	buf.WriteString(splitChar)
	buf.Write(r.Body)
	return buf.String(), true
}
func (r *ResponseCache) Encode(value []byte) {
	split := bytes.SplitN(value, []byte(splitChar), 2)
	if r.Header == nil {
		r.Header = make(map[string]string)

	}
	json.Unmarshal(split[0], &r.Header)
	r.Body = split[1]
}
